﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Privosoft.DataFrame;

namespace Privosoft.DataFrame.UI
{
    class Loader
    {
        ImportStatsInfo Load(string dataFileName, string schemaName)
        {
            var cn = new SqlConnection(Properties.Settings.Default.DBConnectionString);
            using (var db = new Privosoft.DataFrame.DbContext(cn))
            {
                SchemaInfo sch = db.GetSchemaByName(schemaName);
                if (sch == null)
                    throw new LoaderException(String.Format("Schema {0} is not set up", schemaName));

                db.LoadSchemaColumns(sch);
                // 4
                if (sch.Columns.Count == 0)
                    throw new LoaderException(String.Format("Scheme {0} is not set up properly. Please add columns", schemaName));

                return FileReader.ImportCsv(dataFileName, sch, db);
            }
        }
    }
}
